package cn.ffcs.organ.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.ffcs.base.service.BaseService;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.dao.IOrgRelaPersonDAO;
import cn.ffcs.organ.dao.IOrganizationDAO;
import cn.ffcs.organ.dao.IPersonDAO;
import cn.ffcs.organ.dao.OrgRelaPersonDAO;
import cn.ffcs.organ.dao.OrganizationDAO;
import cn.ffcs.organ.dao.PersonDAO;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.security.dao.IUserDAO;
import cn.ffcs.security.dao.UserDAO;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.util.ext.FieldObject;

/**
 * 人员服务<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class PersonServiceForOuter<P extends Person> extends BaseService<P> implements
        IPersonService<P>
{
    /** 人员接口 **/
    private IPersonDAO personDAO;
    /** 组织关系接口 **/
    private IOrgRelaPersonDAO orgRelaPersonDAO;
    /** 用户接口 **/
    private IUserDAO userDAO;
    /** 组织接口 **/
    private IOrganizationDAO organizationDAO;
    
    
    /*
     * 查询未与当前组织关联的所有人员
     */
    public PageControl findSelectByPage(Person person,int currPage, int pageSize,int organId){
        return personDAO.findSelectByPage(person,currPage, pageSize,organId);
    }
    
    /**
     * 
     * 获取当前操作人的本部门人员
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<FieldObject> getCurrDeptPersons(String param){
        // 获取session信息
        ServletContext sc = ApplicationContextUtil.getHttpServletRequest().getSession().getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils
                .getWebApplicationContext(sc);
        //获取注册ｂｅａｎ
        this.orgRelaPersonDAO=(OrgRelaPersonDAO)wac.getBean("OrgRelaPersonDAO");
        this.personDAO=(PersonDAO)wac.getBean("PersonDAO");
        this.userDAO=(UserDAO)wac.getBean("UserDAO");
        
        //获取展示列表对象
        List<FieldObject> result=new ArrayList<FieldObject>();
        
        //获取当前操作人员的信息
        User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        String pID=currUser.getPersonID();
        
        //查找所在部门
        Criterion cond1 = Restrictions.eq("person", currUser.getPerson());  
        List<OrgRelaPerson> orps=this.orgRelaPersonDAO.findBy(cond1);
        if (orps!=null&&orps.size()>0){
            OrgRelaPerson orp=orps.get(0);

            //获取所有人员
            Criterion cond2 = Restrictions.eq("organization", orp.getOrganization());  
            List<OrgRelaPerson> persons=this.orgRelaPersonDAO.findBy(cond2);
            
            for (OrgRelaPerson p:persons){
                FieldObject fo=new FieldObject();
                //设置显示名称
                fo.setName(p.getPerson().getName());
                //查找对应用户ＩＤ
//                Criterion cond3 = Restrictions.eq("person", p.getPerson());  
//                List<User> users=userDAO.findBy(cond3);
//                if (users!=null&&users.size()>0){
//                    User u=users.get(0);
//                    fo.setId(u.getId());
//                } 
                fo.setId(p.getPerson().getId());
                result.add(fo);        
            }
        }
        
        
        return result;
    }
    
    /**
     * 
     * 获取当前操作人的本部门人员
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<FieldObject> getPersonsByDeptId(String param){
        // 获取session信息
        ServletContext sc = ApplicationContextUtil.getHttpServletRequest().getSession().getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils
                .getWebApplicationContext(sc);
        //获取注册ｂｅａｎ
        this.orgRelaPersonDAO=(OrgRelaPersonDAO)wac.getBean("OrgRelaPersonDAO");
        this.organizationDAO=(OrganizationDAO)wac.getBean("OrganizationDAO");
        //获取展示列表对象
        List<FieldObject> result=new ArrayList<FieldObject>();
        //查找所在部门
        Criterion cond1 = Restrictions.eq("name", param);
        List<Organization> organizations=this.organizationDAO.findBy(cond1);
        
        Criterion cond2 = Restrictions.eq("organization", organizations.get(0));
        List<OrgRelaPerson> persons=this.orgRelaPersonDAO.findBy(cond2);
        
        for (OrgRelaPerson p:persons){
            FieldObject fo=new FieldObject();
            //设置显示名称
            fo.setName(p.getPerson().getName());
            fo.setId(p.getPerson().getId());
            result.add(fo);
        }
        return result;
    }
    
    public IPersonDAO getPersonDAO()
    {
        return personDAO;
    }
    public void setPersonDAO(IPersonDAO personDAO)
    {
        this.personDAO = personDAO;
    }

    /**
     * @return the orgRelaPersonDAO
     */
    public IOrgRelaPersonDAO getOrgRelaPersonDAO()
    {
        return orgRelaPersonDAO;
    }

    /**
     * @param orgRelaPersonDAO the orgRelaPersonDAO to set
     */
    public void setOrgRelaPersonDAO(IOrgRelaPersonDAO orgRelaPersonDAO)
    {
        this.orgRelaPersonDAO = orgRelaPersonDAO;
    }

    /**
     * @return the userDAO
     */
    public IUserDAO getUserDAO()
    {
        return userDAO;
    }

    /**
     * @param userDAO the userDAO to set
     */
    public void setUserDAO(IUserDAO userDAO)
    {
        this.userDAO = userDAO;
    }

    public IOrganizationDAO getOrganizationDAO()
    {
        return organizationDAO;
    }

    public void setOrganizationDAO(IOrganizationDAO organizationDAO)
    {
        this.organizationDAO = organizationDAO;
    }
}
